package com.product;

import com.alibaba.fastjson.JSON;
import com.product.pojo.TargetJSON;

import java.util.ArrayList;
import java.util.List;

/**
 * description: TODO 将JSON转换成String返回.
 *
 * @author Pang Xianming
 * @version V1.0
 * @date 2024/10/22 9:51
 * @description <p>将JSON转换成String返回</p>
 **/
public class JSONToString {

    public static void main(String[] args) {
        JSONToString.toString("{\"ChannelInfo\":[{\"Car_List\":[{\"Brand\":\"未知\",\"Color\":\"未知\",\"ID\":\"O2024102209533869200000000000000000023195\",\"Lat\":23.35413714817592,\"Lon\":113.2794240834762,\"Lpn\":\"未知\",\"LpnColor\":\"未知\",\"ObjId\":2781,\"PosX\":965,\"PosY\":601,\"PtcHeading\":241.557071685791,\"Speed\":35.93344497680664,\"Type\":\"未知\",\"VehL\":0,\"VehType\":-1},{\"Brand\":\"福田_奥铃TX-2012款_货车\",\"Color\":\"黑色\",\"ID\":\"O2024102209533407100000000000000000023185\",\"Lat\":23.35405956968969,\"Lon\":113.2792931381537,\"Lpn\":\"粤WK9162\",\"LpnColor\":\"蓝色\",\"ObjId\":2777,\"PosX\":1060,\"PosY\":1044,\"PtcHeading\":241.557071685791,\"Speed\":39.82978439331055,\"Type\":\"未知\",\"VehL\":0,\"VehType\":9}],\"ChannelId\":2,\"Direction\":1,\"DriveWay\":1,\"OtherVehicle_List\":null,\"People_List\":null}],\"DevNo\":\"23\",\"Frame\":501714,\"MsgType\":1000,\"Timestamp\":1729562024870}");

    }
    public static String toString(String json) {
        TargetJSON targetJSON = JSON.parseObject(json, TargetJSON.class);
        List<String> resultList = new ArrayList<>();
        if (targetJSON != null) {
            List<TargetJSON.ChannelInfoBean> channelInfo = targetJSON.getChannelInfo();
            if (channelInfo != null) {
                String devNo = targetJSON.getDevNo();
                String frame = targetJSON.getFrame();
                String msgType = targetJSON.getMsgType();
                String timestamp = targetJSON.getTimestamp();
                channelInfo.stream().iterator().forEachRemaining(channelInfoBean -> {
                    List<TargetJSON.ChannelInfoBean.CarListBean> carList =
                            channelInfoBean.getCarList();
                    if (carList != null) {
                        String channelId = channelInfoBean.getChannelId();
                        String direction = channelInfoBean.getDirection();
                        String driveWay = channelInfoBean.getDriveWay();
                        Object otherVehicleList = channelInfoBean.getOtherVehicleList();
                        Object peopleList = channelInfoBean.getPeopleList();
                        carList.stream().iterator().forEachRemaining(carListBean -> {
                            String str =
                                    carListBean.toString() + channelId + "," + direction + "," +
                                            driveWay + "," + otherVehicleList + "," + peopleList +
                                            "," + devNo + "," + frame + "," + msgType + "," +
                                            timestamp;
                            resultList.add(str);
                        });
                    }
                });
                String result = String.join(";", resultList);
//                System.out.println(result);
                return result;
            }
        }
        return null;
    }
}
